package org.samith.dao;

import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.samith.entity.User;

/**
 * @author Samith Jayawardana
 */
@Stateless
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

	@Override
	public Class<User> getType() {
		return User.class;
	}

	@Override
	public User findByUserName(String username) {
		Query query = em.createQuery("SELECT a FROM User a WHERE a.userName = :u ");
		query.setParameter("u", username);
		User user;
		try{
			user = (User) query.getSingleResult();
		}catch(NoResultException ex){
			System.out.println("=========== No user found ========= ");
			return null;
		}
		return user;
	}

	@Override
	public User findByAuthToken(String authToken) {
		Query query = em.createQuery("SELECT a FROM User a WHERE a.authToken = :u ");
		query.setParameter("u", authToken);
		User user;
		try{
			user = (User) query.getSingleResult();
		}catch(NoResultException ex){
			System.out.println("=========== No user found with authToken "+authToken);
			return null;
		}
		return user;
	}

	@Override
	public User findById(Long id) {
		return read(id);
	}
	
}